package com.letv.core.download.image;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.graphics.Bitmap;
import android.text.TextUtils;

/**
 * 内存
 * 
 * @author zhuqiao
 *
 */
public class SoftMemoryCache implements IMemoryCache {
    private final HashMap<String, SoftReference<Bitmap>> mMemoryCache;

    public SoftMemoryCache() {
        mMemoryCache = new HashMap<String, SoftReference<Bitmap>>();
    }

    @Override
    public void put(String key, Bitmap bitmap) {
        if (!TextUtils.isEmpty(key) && bitmap != null) {
            mMemoryCache.put(key, new SoftReference<Bitmap>(bitmap));
        }
    }

    @Override
    public Bitmap get(String key) {
        if (TextUtils.isEmpty(key))
            return null;
        SoftReference<Bitmap> memBitmap = mMemoryCache.get(key);
        if (memBitmap != null) {
            return memBitmap.get();
        }
        return null;
    }

    @Override
    public void remove(String key) {
        if (!TextUtils.isEmpty(key))
            mMemoryCache.remove(key);
    }

    @Override
    public void clear() {
        mMemoryCache.clear();
    }
}
